package com.cyt.web.controller.work.divideclass;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.divideclass.model.po.DivideclassClass;
import com.cyt.work.divideclass.model.po.DivideclassStudent;
import com.cyt.work.divideclass.model.vo.DivideclassClassVo;
import com.cyt.work.divideclass.service.DivideclassClassService;
import com.cyt.work.divideclass.service.DivideclassStudentService;
import com.cyt.work.enrollment.model.vo.EnrollmentRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 智能分班分班设置-智能分班班级控制层
 *
 * @author caideyong
 * @date 2019-12-20 14:11:03
 */
@RestController
public class DivideclassClassController extends BaseController {

    @Resource
    private DivideclassClassService divideclassClassService;

    @Autowired
    private DivideclassStudentService divideclassStudentService;

    /**
     * 统一跳转
     *
     * @param pageName 页面名称
     * @param applyId  应用id
     * @return Model
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @RequestMapping("/divideclass/divideclassclass/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId,String divideId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclassclass/" + pageName);
        view.addObject("applyId", applyId);
        view.addObject("divideId",divideId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param divideclassClassVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @BaseLog(module = "智能分班分班设置", classz = "智能分班分班设置", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclassclass/page", method = RequestMethod.POST)
    public ReturnResult page(DivideclassClassVo divideclassClassVo, int page, int limit) {
        return divideclassClassService.page(divideclassClassVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @BaseLog(module = "智能分班分班设置", classz = "智能分班分班设置", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclassclass/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id) {
        ModelAndView view = new ModelAndView("/divideclass/divideclassclass/divideclassClassEdit");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(divideclassClassService.selectByPrimaryKey(id));
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param divideclassClassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @BaseLog(module = "智能分班分班设置", classz = "智能分班分班设置", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclassclass/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, DivideclassClassVo divideclassClassVo) {
        return divideclassClassService.addOrUpdate(divideclassClassVo, acceptCurrentUser(request));
    }

    /**
     * 跳转调班界面
     *
     * @param stuId 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @BaseLog(module = "智能分班分班设置", classz = "智能分班分班设置", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclassclass/getChangeClass", method = RequestMethod.GET)
    public ModelAndView getChangeClass(String stuId,String applyId,String divideId) {
        ModelAndView view = new ModelAndView("/work/divideclass/divideclassclass/divideclassClassChange");
        view.addObject("applyId",applyId);
        view.addObject("stuId",stuId);
        view.addObject("divideId",divideId);
        if (StringUtils.isNotBlank(stuId)) {
            String stuIdNew[]=stuId.split(",");
            String name="";
            for(int i=0;i<stuIdNew.length;i++){
                DivideclassStudent divideclassStudent=divideclassStudentService.selectByPrimaryKey(stuIdNew[i]);
                if(i==0){
                    name=divideclassStudent.getName();
                }else{
                    name=name+","+divideclassStudent.getName();
                }
            }
            view.addObject("name",name);
        }
        if(StringUtils.isNotEmpty(divideId)){
            DivideclassClassVo d=new DivideclassClassVo();
            d.setDivideId(divideId);
            List<DivideclassClass> divideclassClassList=divideclassClassService.listExample(d);
            if(divideclassClassList!=null&&divideclassClassList.size()>0){
                view.addObject("classList",divideclassClassList);
            }
        }
        return view;
    }

    /**
     * 批量调班
     *
     * @param request 请求对象
     * @param divideclassClassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @BaseLog(module = "智能分班分班设置", classz = "智能分班分班设置", methods = "批量调班", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclassclass/changeClass", method = RequestMethod.POST)
    public ReturnResult changeClass(HttpServletRequest request, DivideclassClassVo divideclassClassVo) {
        return divideclassClassService.changeClass(divideclassClassVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request 请求对象
     * @param divideclassClassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @BaseLog(module = "智能分班分班设置", classz = "智能分班分班设置", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclassclass/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, DivideclassClassVo divideclassClassVo) {
        return divideclassClassService.remove(divideclassClassVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request 请求对象
     * @param divideclassClassVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-12-20 14:11:03
     */
    @BaseLog(module = "智能分班分班设置", classz = "智能分班分班设置", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/divideclass/divideclassclass/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, DivideclassClassVo divideclassClassVo) {
        return divideclassClassService.delete(divideclassClassVo, acceptCurrentUser(request));
    }

    /**
     * 数据导出
     *
     * @param divideclassClassVo 传入参数对象
     * @param response         响应对象
     * @return void
     * @author caideyong
     * @date 2019/11/27
     */
    @RequestMapping(value = "/work/divideclass/divideclassclass/export")
    public void export(HttpServletResponse response, DivideclassClassVo divideclassClassVo) throws IOException{
        divideclassClassService.export(response, divideclassClassVo);
    }
}